//
//  VM.m
//  corruptEatSerpent
//
//  Created by HYC on 2022/12/5.
//

#import "HYCEatSerpent_VM.h"
#import <HYC_TOOLS_OC/HYC_TOOLS_OC.h>

//#import "HYCEatSerpent_eveModel.h"
//#import "HYCEatSerpent_eveView.h"

@interface HYCEatSerpent_VM()

@property (nonatomic,strong)NSMutableArray <NSMutableArray <HYCEatSerpent_eveView *>*>*bgArr;
@property (nonatomic,strong)NSMutableArray <HYCEatSerpent_eveModel *>*selfArr;
@property (nonatomic,strong)NSMutableArray <NSMutableArray <HYCEatSerpent_eveModel *>*>*allModels;
@property (nonatomic,strong)NSMutableArray <HYCEatSerpent_eveModel *>*blankModels;
@property (nonatomic,strong)HYCEatSerpent_eveModel *foodModel;
@property (nonatomic,assign)BOOL isOnlySwipe;



@property (nonatomic,assign)BOOL isLongTouch;
@end

@implementation HYCEatSerpent_VM


/// 校验是否死亡 , true死亡 , false未死亡
+ (BOOL)verify_isDie:(HYCEatSerpent_eveModel *)model {
    
    if (model.x >= H_Number || model.x < 0 || model.y >= W_Number || model.y < 0)   return true;
    else                                                                            return false;
    
    
}

+ (HYCEatSerpent_VM *)sharedManager
{
    static HYCEatSerpent_VM *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
        
        sharedAccountManagerInstance.runSpeed = runSpeed;
    });
    return sharedAccountManagerInstance;
}
/// 循环运行
- runing:(NSMutableArray <HYCEatSerpent_eveModel *>*)selfArr WithBgArr:(NSMutableArray <NSMutableArray <eveView *>*>*)bgArr {
    
    
    
    self.selfArr = selfArr;
    self.bgArr = bgArr;
    
    CGFloat speed = self.isCrazyMode ? 0.1 : (self.runSpeed - self.foodNumber * 0.01 > 0.1 ? self.runSpeed - self.foodNumber * 0.01 : 0.1);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( speed* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.runingType = [self run];
        
        if (self.runingType == HYC__ing) [self runing:self.selfArr WithBgArr:self.bgArr];
    });
    
    
    return self;
}
/// 运行一次
- (HYC_EatSerpent_runingType)run{
    
    if (self.runingType != HYC__ing) return self.runingType;
    
    HYCEatSerpent_eveModel *startM = self.selfArr.firstObject;
    HYCEatSerpent_eveModel *endM = self.selfArr.lastObject;
    
    HYCEatSerpent_eveModel *nextEveM = [HYCEatSerpent_eveModel initWith_X:startM.x With_Y:startM.y];
    
    if (self.fangxiang == HYC__UP)          nextEveM.x = nextEveM.x - 1;
    else if (self.fangxiang == HYC__DOWN)     nextEveM.x = nextEveM.x + 1;
    else if (self.fangxiang == HYC__LEFT)     nextEveM.y = nextEveM.y - 1;
    else if (self.fangxiang == HYC__RIGHT)     nextEveM.y = nextEveM.y + 1;
    
    //判断是否撞墙
    if ([HYCEatSerpent_VM verify_isDie:nextEveM]) {
        
        
        
        self.runSpeed = runSpeed;
        
        self.openToast ? self.openToast(self.foodNumber):self.openToast;
        
        self.foodNumber = 0;
        return HYC__stop;
    }
    
    //判断是不是吃到了自己
    for (NSInteger i = 0; i < self.selfArr.count; i ++) {
        if (self.selfArr[i].x == nextEveM.x && self.selfArr[i].y == nextEveM.y) {
            
            
            
            
            self.runSpeed = 0.5;
            
            self.openToast ? self.openToast(self.foodNumber):self.openToast;
            
            
            self.foodNumber = 0;
            return HYC__stop;
        }
    }
    
    
    
    
    [self.selfArr insertObject:nextEveM atIndex:0];
    self.bgArr[nextEveM.x][nextEveM.y].colorView.backgroundColor = UIColor.grayColor;

    
    
    //判断是否吃到了食物
    if (self.foodModel.x == nextEveM.x && self.foodModel.y == nextEveM.y) {
        
        self.foodNumber ++;
        
        self.haveFood?self.haveFood(self.foodNumber):self.haveFood;
        [self addFoodWithAllModels:self.allModels];
        
    }else {
        [self.selfArr removeObjectAtIndex:self.selfArr.count - 1];
        self.bgArr[endM.x][endM.y].colorView.backgroundColor = UIColor.whiteColor;
    }
    
    self.isOnlySwipe = false;
    
    return HYC__ing;
}

- (void)addGestSwipe:(UIView *)swipeView{
    
    
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight: )];
    rightSwipe.direction =UISwipeGestureRecognizerDirectionRight;
    [swipeView addGestureRecognizer:rightSwipe];
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:  )];
    leftSwipe.direction =UISwipeGestureRecognizerDirectionLeft;
    [swipeView addGestureRecognizer:leftSwipe];
    UISwipeGestureRecognizer *upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUp:    )];
    upSwipe.direction =UISwipeGestureRecognizerDirectionUp;
    [swipeView addGestureRecognizer:upSwipe];
    UISwipeGestureRecognizer *downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDown:  )];
    downSwipe.direction =UISwipeGestureRecognizerDirectionDown;
    [swipeView addGestureRecognizer:downSwipe];
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchTwo:)];
    tapGesture.numberOfTapsRequired=2;
    [swipeView addGestureRecognizer:tapGesture];
    
}

- (void)addGestLongTouch:(UIView *)longTouchView {
    
    
    [longTouchView addGuest:longAction resutlBlock:^(UITapGestureRecognizer * _Nonnull gesture) {
        
        NSLog(@"%ld",gesture.state);
        self.isLongTouch = (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged);
    }];
}
- (void)touchTwo:(UITapGestureRecognizer *)gest {
    
    [self touchT];
    
}

- touchT {
    
    if (self.runingType == HYC__ing) {
        self.runingType = HYC__pause;
        self.touchTwo?self.touchTwo(true):self.touchTwo;
    }else if (self.runingType == HYC__pause) {
        
        self.runingType = HYC__ing;
        [self runing:self.selfArr WithBgArr:self.bgArr];
    }
    
    return self;
}
- (void)swipeRight:(UISwipeGestureRecognizer *)gestSwipe {
    NSLog(@"right");
    if (self.fangxiang == HYC__LEFT) return;
    if (self.isOnlySwipe) return;
    self.isOnlySwipe = true;
    self.fangxiang = gestSwipe.direction;
}
- (void)swipeLeft:(UISwipeGestureRecognizer *)gestSwipe  {
    NSLog(@"left");
    if (self.fangxiang == HYC__RIGHT) return;
    if (self.isOnlySwipe) return;
    self.isOnlySwipe = true;
    self.fangxiang = gestSwipe.direction;
}
- (void)swipeUp:(UISwipeGestureRecognizer *)gestSwipe    {
    NSLog(@"up");
    if (self.fangxiang == HYC__DOWN) return;
    if (self.isOnlySwipe) return;
    self.isOnlySwipe = true;
    self.fangxiang = gestSwipe.direction;
}
- (void)swipeDown:(UISwipeGestureRecognizer *)gestSwipe  {
    NSLog(@"down");
    if (self.fangxiang == HYC__UP) return;
    if (self.isOnlySwipe) return;
    self.isOnlySwipe = true;
    self.fangxiang = gestSwipe.direction;
}


/// 获取当前空白格坐标
/// - Parameter allModels: 所有的格子坐标
- (NSMutableArray <HYCEatSerpent_eveModel *> *)GeneratingRandomPointWithAllModels:(NSMutableArray <NSMutableArray <HYCEatSerpent_eveModel *>*>*)allModels {
    
    
    for (NSInteger i = 0; i < self.selfArr.count; i ++) for (NSInteger z = 0; z < allModels[self.selfArr[i].x].count; z ++) if (self.selfArr[i].y == allModels[self.selfArr[i].x][z].y) [allModels[self.selfArr[i].x] removeObjectAtIndex:z];

        
        
    
    NSMutableArray <HYCEatSerpent_eveModel *>*returnArr = [NSMutableArray arrayWithCapacity:0];
    for (NSMutableArray *eveArr in allModels) {
        [returnArr addObjectsFromArray:eveArr];
    }
    
    return returnArr;
    
}
- addFoodWithAllModels:(NSMutableArray <NSMutableArray <HYCEatSerpent_eveModel *>*>*)allModels{
    self.allModels = allModels;
    NSMutableArray <NSMutableArray <HYCEatSerpent_eveModel *>*>*all = [NSMutableArray arrayWithCapacity:0];
    for (NSMutableArray *array in self.allModels) {
        NSMutableArray *eve = [NSMutableArray arrayWithArray:array];
        [all addObject:eve];
    }
    
    
    
    //获取食物点坐标
    self.blankModels = [HYCEatSerpent_VM.sharedManager GeneratingRandomPointWithAllModels:all];
    
    self.foodModel = self.blankModels[arc4random() % self.blankModels.count];
    
    self.bgArr[self.foodModel.x][self.foodModel.y].colorView.backgroundColor = UIColor.redColor;
    
    return self;
}

- (void)setIsLongTouch:(BOOL)isLongTouch {
    _isLongTouch = isLongTouch;
    static CGFloat runSpeedOld = 0;
    
    if (_isLongTouch) {
        runSpeedOld = self.runSpeed == 0.1 ? runSpeedOld : self.runSpeed;
        self.runSpeed = 0.1;
        
    }else {
        
        self.runSpeed = runSpeedOld;
    }
    
    
}

@end
